package com.iaiai.imail.core;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iaiai.imail.activity.R;
import com.iaiai.imail.util.Utils;

/**
 * 
 * <p>
 * Title: IActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-8-20 上午10:40:10
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public abstract class IActivity extends Activity {

	protected interface Handler {
		void query();
	}

	private Bundle bundle;

	protected ProgressDialog dialog; // Loading...

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	protected void setTitleText(String title) {
		((TextView) findViewById(R.id.title_text)).setText(title);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		bundle = getIntent().getExtras();

		dialog = new ProgressDialog(this);
		dialog.setMessage(getResources().getString(R.string.loading));
	}

	/**
	 * 返回标题栏布局
	 * 
	 * @return
	 */
	protected View getTitleLayout() {
		return findViewById(R.id.title_layout);
	}

	/**
	 * 在另一线程中执行程序
	 * 
	 * @param runnable
	 */
	protected void execHandle(final Handler handler, final Runnable runnable) {
		final android.os.Handler oaHandler = new android.os.Handler();
		new Thread() {
			public void run() {
				handler.query();
				oaHandler.post(runnable);
			}
		}.start();
	}

	/**
	 * 在另一线程中执行程序
	 * 
	 * @param task
	 * @param delay
	 *            多长时间之后运行程序
	 */
	protected void execHandle(final TimerTask task, long delay) {
		Timer timer = new Timer(true);
		timer.schedule(task, delay);
	}

	/**
	 * 在另一线程中执行程序
	 * 
	 * @param task
	 * @param delay
	 *            多长时间之后运行程序
	 */
	protected void execHandle(final TimerTask task, long delay, long period) {
		Timer timer = new Timer(true);
		timer.schedule(task, delay, period);
	}

	/**
	 * 获得Dao
	 * 
	 * @return
	 */
	protected IServiceFactory getDao() {
		return IServiceFactory.getInstance();
	}

	public Bundle getBundle() {
		if (bundle == null) {
			return new Bundle();
		}
		return bundle;
	}

	protected void insertTitleLayoutButton(int imageId, OnClickListener listener) {
		LinearLayout layout = (LinearLayout) findViewById(R.id.title_layout);

		LinearLayout line = new LinearLayout(this);
		line.setLayoutParams(new LayoutParams(new LayoutParams(Utils.dipTopx(this, 1),LayoutParams.FILL_PARENT)));
		line.setBackgroundColor(android.R.color.white);

		layout.addView(line);

		LinearLayout l = new LinearLayout(this);
		l.setOrientation(LinearLayout.VERTICAL);
		l.setLayoutParams(new LayoutParams(Utils.dipTopx(this, 44), LayoutParams.FILL_PARENT));
		l.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);

		ImageView image = new ImageView(this);
		image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		image.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_return));
		l.addView(image);
		l.setOnClickListener(listener);

		layout.addView(l);
	}

}
